package com.ruoyi.job.task;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.GeTuiResult;
import com.ruoyi.common.core.utils.GeTuiUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.job.domain.GeTuiToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component("geTuiTask")
public class GetTuiTask {

    @Autowired
    private RedisService redisService;


    /**
     * 获取推送token
     */
    public void getTokenTask(String appkey, String masterSecret, String baseUrl){
       GeTuiResult result = GeTuiUtils.getToken(appkey, masterSecret, baseUrl);
       if (result.getCode() == 0){
           JSONObject t = (JSONObject) result.getData();
           GeTuiToken tuiToken =  t.toJavaObject(GeTuiToken.class);
           String token = tuiToken.getToken();
           System.out.println("getui: " + token);
           String expireTime = tuiToken.getExpire_time();
           redisService.setCacheObject(appkey, token, Long.parseLong(expireTime), TimeUnit.MILLISECONDS);
       }
    }
}
